package com.changgou.service.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌列表查询
     *
     * @return
     */
    @Override
    public List<Brand> findList() {

        return lambdaQuery().list();
    }

    /**
     * 根据id查询品牌信息
     *
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return lambdaQuery().eq(Brand::getId, id).one();

    }

    /**
     * 品牌新增
     *
     * @param brand
     */
    @Override
    @Transactional
    public void add(Brand brand) {
        this.save(brand);
    }

    /**
     * 品牌修改
     *
     * @param brand
     */
    @Override
    @Transactional
    public void update(Brand brand) {
        this.updateById(brand);
    }

    /**
     * 根据id删除品牌信息
     *
     * @param id
     */
    @Override
    @Transactional
    public void delById(Integer id) {
        this.removeById(id);
    }

    /**
     * 品牌列表条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Brand> list(Map<String, Object> searchMap) {
        return brandMapper.selectList(Wrappers.<Brand>lambdaQuery()
                .like(searchMap.get("name") != null, Brand::getName, searchMap.get("name"))
                .eq(searchMap.get("letter") != null, Brand::getLetter, searchMap.get("letter")));
    }

    /**
     * 品牌列表分页查询
     *
     * @param page
     * @param size
     */

    @Override
    public Page<Brand> findPage(int page, int size) {
        Page<Brand> pages = (Page<Brand>) (lambdaQuery().page(new Page<>(page, size)));
        return pages;
    }
/**
 * 品牌列表条件分页查询
 * @param searchMap 查询条件
 * @param page 页码
 * @param size 每页显示条数
 * @return
 */
    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        Page<Brand> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<Brand> wrapper = Wrappers.<Brand>lambdaQuery()
                .like(searchMap.get("name") != null, Brand::getName, searchMap.get("name"))
                .eq(searchMap.get("letter") != null, Brand::getLetter, searchMap.get("letter"));
        Page<Brand> brandPage = (Page<Brand>) brandMapper.selectPage(pageInfo, wrapper);
        return brandPage;
    }
    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }
}
